/*******************************************************************************
 * Copyright (c) 2003 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.api;

import java.util.HashMap;
import java.util.Map;

/**
 * @author bpruessm
 * 
 *         To change the template for this generated type comment go to Window -
 *         Preferences - Java - Code Generation - Code and Comments
 */
@SuppressWarnings("nls")
public class CharSetEnum {

  public static final CharSetEnum NOCONV = new CharSetEnum(0, "NOCONV");

  public static final CharSetEnum UTF_8 = new CharSetEnum(1, "UTF_8");

  public static final CharSetEnum ISO8859_1 = new CharSetEnum(2, "ISO8859_1");

  public static final CharSetEnum UTF_16 = new CharSetEnum(3, "UTF_16");

  public static final CharSetEnum SHIFTJIS = new CharSetEnum(4, "SHIFTJIS");

  public static final CharSetEnum EUCJP = new CharSetEnum(5, "EUCJP");

  public static final CharSetEnum WIN_US_ANSI = new CharSetEnum(6, "WIN_US_ANSI");
  public static final CharSetEnum WIN_US_OEM = new CharSetEnum(7, "WIN_US_OEM");

  public static final CharSetEnum MACOS_ROMAN = new CharSetEnum(8, "MACOS_ROMAN");

  /** Stores mapping between string and enum. */
  private static Map<String, CharSetEnum> stringToEnumMap;

  /** Stores the enum's id. */
  private int id;

  /** Stores the enum's description. */
  private String description;

  /**
   * Constructor for CharSetEnum.
   * 
   * @param theId the enum id.
   * @param theDescription the description.
   */
  private CharSetEnum(int theId, String theDescription) {
    id = theId;
    description = theDescription;

    if (stringToEnumMap == null) {
      stringToEnumMap = new HashMap<String, CharSetEnum>();
    }

    stringToEnumMap.put(description, this);
  }

  /**
   * Called to lookup the CharSetEnum for the given string.
   * 
   * @param charSetString the string to lookup.
   * @return the CharSetEnum. NOCONV if no matching CharSetEnum was found.
   */
  public static CharSetEnum fromString(String charSetString) {
    CharSetEnum result = stringToEnumMap.get(charSetString);
    if (result == null) {
      result = CharSetEnum.NOCONV;
    }

    return result;
  }

  /**
   * Returns the enum's id.
   * 
   * @return the enum's id.
   */
  public int getId() {
    return id;
  }

  @Override
  public String toString() {
    return description;
  }
}
